(define-module (python youtube-local)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix build-system python)
  #:use-module (gnu packages)
  #:use-module (gnu packages python)
  #:use-module (gnu packages python-compression)
  #:use-module (gnu packages python-web)
  #:use-module (gnu packages python-xyz)
  #:use-module (gnu packages xml)
  #:use-module (guix packages)
  #:use-module (guix download)
  #:use-module (guix git-download))

(define-public python-cachetools
  (package
    (name "python-cachetools")
    (version "4.1.1")
    (source
     (origin
       (method url-fetch)
       (uri (pypi-uri "cachetools" version))
       (sha256
        (base32
         "086vkqy084rd0xlfnyyylwr2vp8qrz806aywy9fif06yvv1kkamv"))))
    (build-system python-build-system)
    (home-page "https://github.com/tkem/cachetools/")
    (synopsis
     "Extensible memoizing collections and decorators")
    (description
     "Extensible memoizing collections and decorators")
    (license license:expat)))


(define-public youtube-local
  (package
    (name "youtube-local")
    (version "2.3.2")
    (source (origin
              (method git-fetch)
              (uri (git-reference
                    (url "https://github.com/user234683/youtube-local.git")
                    (commit (string-append "v" version))))
              (file-name (git-file-name name version))
              (sha256
               (base32
                "19ynbszn7mm7w7p48xmg372y4n4gq3q4krli0fixqkvabzc5n987"))
              (modules '((guix build utils)))
              (snippet
               '(begin
                  (substitute* "server.py"
                    (("from gevent import monkey")
                     "#!/usr/bin/env python3\nfrom gevent import monkey"))))))
    (build-system python-build-system)
    (arguments
     `(#:phases
       (modify-phases %standard-phases
         (delete 'build)
         (delete 'check)
         (replace 'install
           (lambda* (#:key outputs #:allow-other-keys)
             (let* ((source (assoc-ref %build-inputs "source"))
                    (out (assoc-ref outputs "out"))
                    (bin (string-append out "/bin"))
                    (target (string-append bin "/youtube-local-server"))
                    (share (string-append out "/share"))
                    (yt (string-append out "/share/youtube")))
               (mkdir-p yt)
               (copy-recursively "youtube" yt)
               (install-file "settings.py" share)
               (mkdir-p bin)
               (copy-file "server.py" target)
               (chmod target #o555)
               (with-directory-excursion bin
                 (setenv "PYTHONPATH"
                         (string-append share
                                        ":"
                                        (getenv "PYTHONPATH")))
                 (wrap-program target
                   `("PYTHONPATH" = (,(getenv "PYTHONPATH")))))
               #t))))))
    (inputs
     `(("python" ,python)
       ("python-brotli" ,python-brotli)
       ("python-gevent" ,python-gevent)
       ("python-flask" ,python-flask)
       ("python-pysocks" ,python-pysocks)
       ("python-urllib3" ,python-urllib3)
       ("python-defusedxml" ,python-defusedxml)
       ("python-cachetools" ,python-cachetools)))
    (home-page "https://github.com/user234683/youtube-local")
    (synopsis "Browser-based client for watching Youtube")
    (description "youtube-local is a browser-based client written in
Python for watching Youtube anonymously and without the lag of the
slow page used by Youtube.")
    (license license:agpl3)))

youtube-local
